 function [best_pop_x,Error] = my_ik_theta7(target,theta7,tool)

%初始参数

dim=6;
tmax=500;
m=100;

c1=2;
c2=2;
% for i=1:tmax
% k(i)=0.05*exp(i/tmax);
% end
k=0.1;

%目标关节位置

%初始化种群限制
vlim=zeros(6,2);
% qlim=[-140,-5;-5,180;-120,155;-350,350;-125,125;-350,350;-180,180 ];
qlim=[-185,185;-140 -5;-120,155;-90,90;-125,125;-90,90];%;-180,180];
% qlim=[-5.3,5.3; -85.6,85.6;-32.2,32.2;-33.6,33.6;-36.8,36.9;-42.6,42.6;-64.7,64.7];
vlim(:,1)=-k*qlim(:,2);
vlim(:,2)=k*qlim(:,2);
% vlim(1,:)=-vlim(1,:);

%初始化种群速度和位置
x=zeros(m,dim);
v=zeros(m,dim);
for i=1:m
    x(i,1)=rand*(qlim(1,2)-qlim(1,1))+qlim(1,1);
    v(i,1)=rand*(vlim(1,2)-vlim(1,1))+vlim(1,1);
    x(i,2)=rand*(qlim(2,2)-qlim(2,1))+qlim(2,1);
    v(i,2)=rand*(vlim(2,2)-vlim(2,1))+vlim(2,1);
    x(i,3)=rand*(qlim(3,2)-qlim(3,1))+qlim(3,1);
    v(i,3)=rand*(vlim(3,2)-vlim(3,1))+vlim(3,1);
    x(i,4)=rand*(qlim(4,2)-qlim(4,1))+qlim(4,1);
    v(i,4)=rand*(vlim(4,2)-vlim(4,1))+vlim(4,1);
    x(i,5)=rand*(qlim(5,2)-qlim(5,1))+qlim(5,1);
    v(i,5)=rand*(vlim(5,2)-vlim(5,1))+vlim(5,1);
    x(i,6)=rand*(qlim(6,2)-qlim(6,1))+qlim(6,1);
    v(i,6)=rand*(vlim(6,2)-vlim(6,1))+vlim(6,1);
end

%第一代最佳个体和最佳全局个体
ind_fit=zeros(m,1);
for i=1:m
    ind_fit(i)=fitness([x(i,:),theta7],target,tool);
end
best_ind_x=x;
[pop_fit,c]=min(ind_fit);
best_pop_x=x(c,:);
Error=fitness( [best_pop_x,theta7],target,tool);
t=1;
x_fit=zeros(m,1);
%开始迭代


    while t<=tmax
        w1=exp(-(t/tmax)^(rand));
        w2=exp(-t^(3)/tmax);
        for i=1:m
            %                         v(i,:)=w1*v(i,:)+c1*rand(1,7).*(best_ind_x(i,:)-x(i,:))+c2*rand(1,7).*(best_pop_x-x(i,:));
            v(i,:)=w1*v(i,:)+c1*rand*(best_ind_x(i,:)-x(i,:))+c2*rand*(best_pop_x-x(i,:));
            x(i,:)=x(i,:)+v(i,:);
            for j=1:dim
                if v(i,j)>vlim(j,2)||v(i,j)<vlim(j,1)
                    v(i,j)=rand*(vlim(j,2)-vlim(j,1))+vlim(j,1);
                end
                if x(i,j)>qlim(j,2)||x(i,j)<qlim(j,1)
                    x(i,j)=rand*(qlim(j,2)-qlim(j,1))+qlim(j,1);
                end
            end
            x_fit(i)=fitness([x(i,:),theta7],target,tool);
            if x_fit(i)<ind_fit(i)
                ind_fit(i)=x_fit(i);
                best_ind_x(i,:)=x(i,:);
            end
            if x_fit(i)<pop_fit
                pop_fit=x_fit(i);
                best_pop_x=x(i,:);
            end
        end
        %变异
        for i=1:m
            if rand<1-w2
                r = round(1 + (m-1)*rand,0);
                if r==i
                    r = round(1 + (m-1)*rand,0);
                end
                x(i,:)=best_pop_x+2*rand*(best_ind_x(r,:)-best_ind_x(i,:));
                %                         x(i,:)=best_pop_x+2*rand(7,1).*((best_ind_x(r,:)-best_ind_x(i,:));
            else
                %             x(i,:)=rand*best_pop_x+w2*randn;
                x(i,:)=rand*best_pop_x+w2*randn;
            end
            for j=1:dim
                if x(i,j)>qlim(j,2)||x(i,j)<qlim(j,1)
                    x(i,j)=rand*(qlim(j,2)-qlim(j,1))+qlim(j,1);
                end
            end
            x_fit(i)=fitness([x(i,:),theta7],target,tool);
            if x_fit(i)<ind_fit(i)
                ind_fit(i)=x_fit(i);
                best_ind_x(i,:)=x(i,:);
            end
            if x_fit(i)<pop_fit
                pop_fit=x_fit(i);
                best_pop_x=x(i,:);
            end
        end
        Error=fitness( [best_pop_x,theta7],target,tool);
        t=t+1;
    end
% disp( [' Evaluations = ' num2str([best_pop_x,theta7])  ', Error = ' num2str(Error)]);

end

